package net.infamily.web;

import javax.servlet.http.HttpServletRequest;

import net.infamily.logic.UserLogic;
import net.infamily.web.pages.LoginPage;
import net.infamily.web.pages.LogoutPage;
import net.infamily.web.pages.RegistrationPage;
import net.infamily.web.secure.ProfilePage;

import org.apache.wicket.Page;
import org.apache.wicket.authentication.AuthenticatedWebApplication;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.HttpSessionStore;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.session.ISessionStore;

import com.google.inject.Inject;

public class MyWebApp extends AuthenticatedWebApplication {
	private NonThreadedModificationWatcher resourceWatcher = new NonThreadedModificationWatcher();

	@Inject
	private UserLogic loginLogic;
	
	public MyWebApp() {
		WebApplication.set(this);
	}
	
	@Override
	protected void init() {
		// Enable non threaded modification watching (required for GAE)
		getResourceSettings().setResourceWatcher(resourceWatcher);
		getDebugSettings().setDevelopmentUtilitiesEnabled(true);

		getMarkupSettings().setAutomaticLinking(true);
		mountBookmarkablePage("/register", RegistrationPage.class);
		mountBookmarkablePage("/profile", ProfilePage.class);
		mountBookmarkablePage("/login", LoginPage.class);
		mountBookmarkablePage("/logout", LogoutPage.class);
		super.init();
	}

	@Override
	protected ISessionStore newSessionStore() {
		// Disable SecondLevelCacheSessionStore (required for GAE)
		return new HttpSessionStore(this);
	}

	@Override
	public Class<? extends Page> getHomePage() {
		return ProfilePage.class;
	}

	@Override
	protected Class<? extends WebPage> getSignInPageClass() {
		return LoginPage.class;
	}

	@Override
	protected WebRequest newWebRequest(HttpServletRequest servletRequest) {
		resourceWatcher.checkForModifications();
		return super.newWebRequest(servletRequest);
	}
	
	@Override
	protected Class<? extends AuthenticatedWebSession> getWebSessionClass() {
		return MyWebSession.class;
	}

	public UserLogic getLoginLogic() {
		return loginLogic;
	}
}
